package co.fitstart.admin.entity.search;

import java.util.Date;
import java.util.Map;

import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;

/**
 * 
 * @author haiyan_xiao
 *
 */
public class TelephoneRecordSearch implements QueryBuilder {

    /** 门店 */
    private Site site;
    
    /** 首次接待人 */
    private Principal receptionist;
    
    /** 开始时间 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date begin;
    
    /** 结束时间 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date end;
    
    /** 是否到店 */
    private Boolean experienced;
    
    private Order order;

    private Filter filter;
    
    public TelephoneRecordSearch(TelephoneResourceSearch telephoneResourceSearch) {
        this.site = telephoneResourceSearch.getSite();
        this.receptionist = telephoneResourceSearch.getReceptionist();
        this.begin = telephoneResourceSearch.getBegin();
        this.end = telephoneResourceSearch.getEnd();
        this.experienced = telephoneResourceSearch.getExperienced();
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {
        if(filter == null || refresh) {
            filter =  new Filter().addEquals("disabled", false)
                                  .addEquals("telephoneResource.disabled", false)
                                  .addEquals("telephoneResource.site", site)
                                  .addEquals("telephoneResource.receptionist", receptionist)
                                  .addDateBetween("telephoneResource.collectDate", begin, end)
                                  .addEquals("telephoneResource.experienced", experienced);
        }
        
        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        return "SELECT new Map(telephoneResource.id AS id, COUNT(id) AS total) FROM TelephoneRecord " + getPreparedFilter(false).getWhereSentence();
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }

    /**
     * @return the site
     */
    public Site getSite() {
        return site;
    }

    /**
     * @param site the site to set
     */
    public void setSite(Site site) {
        this.site = site;
    }

    /**
     * @return the receptionist
     */
    public Principal getReceptionist() {
        return receptionist;
    }

    /**
     * @param receptionist the receptionist to set
     */
    public void setReceptionist(Principal receptionist) {
        this.receptionist = receptionist;
    }

    /**
     * @return the begin
     */
    public Date getBegin() {
        return begin;
    }

    /**
     * @param begin the begin to set
     */
    public void setBegin(Date begin) {
        this.begin = begin;
    }

    /**
     * @return the end
     */
    public Date getEnd() {
        return end;
    }

    /**
     * @param end the end to set
     */
    public void setEnd(Date end) {
        this.end = end;
    }

    /**
     * @return the experienced
     */
    public Boolean getExperienced() {
        return experienced;
    }

    /**
     * @param experienced the experienced to set
     */
    public void setExperienced(Boolean experienced) {
        this.experienced = experienced;
    }
}
